<?php

namespace App\Http\Controllers\Api\Customer;

use Illuminate\Http\Request;
use App\Jobs\OrderSubmit;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Carbon\Carbon;
use App\Repository\UpThree\MainOrderRepostition;
use App\Http\Controllers\Api\BaseController;

class ZheJiangTaiZhouTelecomController extends BaseController
{
  protected $log = null;
  public function __construct()
  {
    $this->log = new Logger('ZheJiangTaiZhouTelecomPush');
    $this->log->pushHandler(
      (new RotatingFileHandler(storage_path('/logs/ZheJiangTaiZhouTelecomPush.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
    );
  }
  // 电信那边推送过来的数据 
  public function pushOrderStatus(Request $Request)
  {
    // 刘志恒 让取消发货等状态接受
    // return json_encode(['responseCode' => 0, 'result' => '接受成功']);
    $getData = file_get_contents('php://input');
    $this->log->info('input' . ($getData));
    // $getData = '{"archived_flag":"","groupOrder":"690000000001008323061944451475","status_cn":"待反刷","queAgTime":0,"photo_step":"","sign":"e9eeb5a7a5376676048dd1e471a36998","orderNum":"T23061909072844089","delivery_time":"","message":"","srcsysid":"qqsmFX100","logistics_status":"已投妥","outOrderNum":"HK_6919442363274302964","card_num":"","waybill_number":"","orderBackMessage_time":"","waistJdOrder":"1","clientip":"127.0.0.1","uim":"","order_message":"B2I通知：反刷成功","logistics_company":"","inputtime":"20230621145645","consignor":"","status":"69"}';
    // return json_encode(['status'=>200]);
    // return json_encode(['code' => 0, 'success' => true]);
    // 数据处理  
    if (empty($getData)) {
      return json_encode(['code' => 103, 'success' => false]);
    }
    if (!empty($getData) && is_string($getData)) {
      $getData = json_decode($getData, true);
      // var_dump($getData, 444444444);
    }
    // var_dump($getData,11111111);
    if (empty($getData)) {
      return json_encode(['status'=>201]);
      return json_encode(['code' => 104, 'result' => false]);
    }
    $statusDatas = [
      0 => '提交中',
      1 => '提交上游',
      // 2 => '拒收单',
      10 => '审核中',
      11 => '待发货',
      20 => '已发货',
      30 => '已签收',
      40 => '已激活',
      50 => '异常单',
      51 => '取消单',
      60 => '失败单'
    ];
    // 100：初始状态,200：待支付,201：支付中,202：支付成功,203：支付失败,300：待订购,301：订购中,302：订购成功,303：订购失败,304：订购待重试,1000：待商户确认,1005：预处理成功,1010：预处理失败,1015：备货中,1020：已发货,1025：订购完成,1030：订购失败,1035：已激活,1040：激活失败，20：异步收单下单失败，其中302、303、1030、1035、20是订购终态
    $returnStatusArr = array(
      'pending' => 1,
      'processing' => 10,
      'completed' => 20,
      'Order_cancel' => 51,
      'order_cancel' => 51,
      'fail' => 60,
      'active_success' => 40
    );
    $status = 1;
    if (!empty($getData['status'])) {
      if($getData['status'] >= 1 && $getData['status'] <= 59){
        $status = 11;
      }else if($getData['status'] >= 60 && $getData['status'] <= 62) {
        $status = 20;
      }else if($getData['status'] >= 63 && $getData['status'] <= 70) {
        $status = 40;
      }
      if($getData['status'] == 65){
        $status = 50;
      }
      if($getData['status'] == 74){
        $status = 51;
      }
    }
    
    // 将数据格式 修改成固定的数据格式
    $editData = [
      'status' => '',
      // 'express_company' => !empty($getData['sendNo']) ? $getData['sendNo'] : '',
      'express_company' => !empty($getData['logistics_company']) ? $getData['logistics_company'] : '',
      'express_number' => !empty($getData['waybill_number']) ? $getData['waybill_number'] : '',
      'three_msg' => !empty($getData['order_message']) ? $getData['order_message'] : '',
      'iccid' => !empty($getData['card_num']) ? $getData['card_num'] : '',
      'activat_at' => !empty($getData['group_orderidstatusuptime']) ? $getData['group_orderidstatusuptime'] : '',
      'express_desc' => !empty($getData['express_desc'])?$getData['express_desc']:'',
      'card_status_desc' => !empty($getData['status_cn'])?$getData['status_cn']:''
    ];
    if (!empty($status)) {
      $editData['status'] = $status;
    }
    if ($status < 20 && !empty($getData['logistics_company']) && !empty($getData['waybill_number'])) {
      $editData['status'] = 20;
    }
    if (empty($editData['status'])) {
      return json_encode(['status'=>202]);
      return json_encode(['responseCode' => 1, 'result' => '未查询到状态']);;
    }
    // var_dump($editData,444444444);
    $where = ['ordersno' => $getData['outOrderNum']];
    $model = new MainOrderRepostition();
    $res = $model->followOrderStatus($where, $editData);
    // var_dump($res);
    $this->log->info('通知参数' . json_encode($getData) . '返回参数' . json_encode($res));
    if (!empty($res['code']) && $res['code'] == 200) {
      return json_encode(['status'=>200]);
      return json_encode(['responseCode' => 0, 'result' => '接受成功']);
      return 'success';
    }
    return json_encode(['status'=>102]);
    return json_encode(['responseCode' => 105, 'result' => '操作失败']);
    return 'error';
  }
}
